package com.newegg.datafeed.adserving.adwords;

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.google.api.ads.adwords.axis.utils.v201809.SelectorBuilder;
import com.google.api.ads.adwords.axis.v201809.cm.AdvertisingChannelType;
import com.google.api.ads.adwords.axis.v201809.cm.ApiException;
import com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyConfiguration;
import com.google.api.ads.adwords.axis.v201809.cm.BiddingStrategyType;
import com.google.api.ads.adwords.axis.v201809.cm.Budget;
import com.google.api.ads.adwords.axis.v201809.cm.BudgetBudgetDeliveryMethod;
import com.google.api.ads.adwords.axis.v201809.cm.BudgetOperation;
import com.google.api.ads.adwords.axis.v201809.cm.BudgetServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.Campaign;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignOperation;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignPage;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignReturnValue;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignServiceInterface;
import com.google.api.ads.adwords.axis.v201809.cm.CampaignStatus;
import com.google.api.ads.adwords.axis.v201809.cm.ManualCpcBiddingScheme;
import com.google.api.ads.adwords.axis.v201809.cm.Money;
import com.google.api.ads.adwords.axis.v201809.cm.NetworkSetting;
import com.google.api.ads.adwords.axis.v201809.cm.Operator;
import com.google.api.ads.adwords.axis.v201809.cm.Selector;
import com.google.api.ads.adwords.lib.selectorfields.v201809.cm.CampaignField;
import com.google.api.ads.common.lib.exception.ValidationException;

public class CampaignService extends ADWordsParent{
	
	CampaignServiceInterface campaignService;
	BudgetServiceInterface budgetService;
	
	public CampaignService(String customerId) throws ValidationException {
		campaignService = get(customerId, CampaignServiceInterface.class);
		budgetService = get(customerId, BudgetServiceInterface.class);
	}
	
	public Long addCampaign(String name) throws ApiException, RemoteException {
		Campaign campaign = new Campaign();
		campaign.setName(name);
		campaign.setStatus(CampaignStatus.PAUSED);
		campaign.setStartDate("20181207");
		campaign.setEndDate("20181208");
		
		/**
		 * 设置广告的费用信息
		 */
	    Budget budget = new Budget();
	    budget.setName("test budget:" + System.currentTimeMillis());
	    Money budgetAmount = new Money();
	    budgetAmount.setMicroAmount(1_000_000L);
	    budget.setAmount(budgetAmount);
	    budget.setDeliveryMethod(BudgetBudgetDeliveryMethod.STANDARD);
	    BudgetOperation budgetOperation = new BudgetOperation();
	    budgetOperation.setOperand(budget);
	    budgetOperation.setOperator(Operator.ADD);
	    Long budgetId = budgetService.mutate(new BudgetOperation[] {budgetOperation}).getValue(0).getBudgetId();
	    budget.setBudgetId(budgetId);
	    campaign.setBudget(budget);
	    
	    /**
	     * 设置广告的投放类型
	     */
	    campaign.setAdvertisingChannelType(AdvertisingChannelType.SEARCH);
	    
	    /**
	     * 设置广告的投放渠道
	     */
	    NetworkSetting networkSetting = new NetworkSetting();
	    networkSetting.setTargetGoogleSearch(true);
	    networkSetting.setTargetSearchNetwork(true);
	    networkSetting.setTargetContentNetwork(false);
	    networkSetting.setTargetPartnerSearchNetwork(false);
	    campaign.setNetworkSetting(networkSetting);
	    
	    BiddingStrategyConfiguration biddingStrategyConfiguration = new BiddingStrategyConfiguration();
	    biddingStrategyConfiguration.setBiddingStrategyType(BiddingStrategyType.MANUAL_CPC);
	    biddingStrategyConfiguration.setBiddingScheme(new ManualCpcBiddingScheme());
	    campaign.setBiddingStrategyConfiguration(biddingStrategyConfiguration);
	    
		CampaignOperation operations = new CampaignOperation();
		operations.setOperator(Operator.ADD);
		operations.setOperand(campaign);
		CampaignReturnValue returnd = campaignService.mutate(new CampaignOperation[]{operations});
		Campaign newCampaign = returnd.getValue(0);
		return newCampaign.getId();
	}
	
	
	public List<Campaign> listCampaign() throws ApiException, RemoteException {
	  int offset = 0;
	  int PAGE_SIZE = 100;

	  // Create selector.
	  SelectorBuilder builder = new SelectorBuilder();
	  Selector selector = builder
	      .fields(CampaignField.Id, CampaignField.Name)
	      .orderAscBy(CampaignField.Name)
	      .offset(offset)
	      .limit(PAGE_SIZE)
	      .build();

	  CampaignPage page;
	  List<Campaign> campaignList = new ArrayList<>();
	  do {
	    // Get all campaigns.
	    page = campaignService.get(selector);

	    // Display campaigns.
	    if (page.getEntries() != null) {
	      campaignList.addAll(Arrays.asList(page.getEntries()));
	    }

	    offset += PAGE_SIZE;
	    selector = builder.increaseOffsetBy(PAGE_SIZE).build();
	  } while (offset < page.getTotalNumEntries());
	  return campaignList;
	}
	
	
	public static void main(String[] args) throws ApiException, RemoteException, ValidationException {
		long id = new CampaignService("698-254-6209").addCampaign("test add compaign");
		System.out.println(id);
	}
}
